﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crude.VisualStudioAddin.Actions
{
    public abstract class BaseProjectAction : IVSAction
    {
        public enum ProjectCrudeStatus
        {
            Both, None, Crude
        }

        public bool AddToSolution
        {
            get
            {
                return false;
            }
           
        }

        public bool AddToProject
        {
            get
            {
                return true;
            }
        }


        public abstract string Name
        {
            get;
        }

        public abstract string Caption
        {
            get;
        }

        public abstract string Tooltip
        {
            get;
        }

        public abstract ProjectCrudeStatus CrudeStatus
        {
            get;
        }        

        public void GetActionStatus(EnvDTE80.DTE2 applicationObject, out bool visible, out bool enable, out string caption)
        {
            object[] selectedProjects = (object[])applicationObject.ActiveSolutionProjects;

            if (!applicationObject.Solution.IsOpen)
            {
                visible = false;
                enable = false;
                caption = null;
            }
            else if (selectedProjects.Length == 0)
            {
                visible = true;
                enable = false;
                caption = null;
            }
            else
            {
                try
                {
                    EnvDTE.Project project = (EnvDTE.Project)selectedProjects[0];

                    Core.Project crudeProject = new Core.Project(project.FullName);

                    if (CrudeStatus == ProjectCrudeStatus.Both || (crudeProject.IsImportLineExisted() && 
                        CrudeStatus == ProjectCrudeStatus.Crude) ||
                        (!crudeProject.IsImportLineExisted() &&
                        CrudeStatus == ProjectCrudeStatus.None))
                    {
                        visible = true;
                        enable = true;
                    }
                    else
                    {
                        visible = true;
                        enable = false;
                    }
                }
                catch
                {
                    visible = true;
                    enable = false;
                }

                caption = null;
            }
        }

        public void ExecuteAction(EnvDTE80.DTE2 applicationObject)
        {
            object[] selectedProjects = (object[])applicationObject.ActiveSolutionProjects;

            if (selectedProjects.Length > 0)
            {
                EnvDTE.Project project = (EnvDTE.Project)selectedProjects[0];

                InternalExecuteAction(applicationObject, project);
            }
        }

        protected abstract void InternalExecuteAction(EnvDTE80.DTE2 applicationObject, EnvDTE.Project project);



      
    }
}
